package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //涉及到表dish_flavor导入相对应的service
    @Autowired
    private DishFlavorService dishFlavorService;


    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //第一张表保存到dish表中
        this.save(dishDto);

        //第二张表保存在，注意 dishDto中的list<DishFlavor>只有name和value值 没有categoryId值
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    @Transactional
    public DishDto getWithFlavor(Long id) {
        //查询第一张表中的数据
        Dish dish = this.getById(id);
        //设置DishFlavor表中的id查询出对应的菜品信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);

        List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

        DishDto dishDto = new DishDto();
        //拷贝信息到dishDto对象中
        BeanUtils.copyProperties(dish,dishDto);
        //添加setFlavors方法完善信息
        dishDto.setFlavors(dishFlavorList);
        //返回
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //保存时要涉及到两张表
        //1.dish表直接修改
        this.updateById(dishDto);

        //第二张表dish_flavor
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        /*LambdaUpdateWrapper<DishFlavor> dishFlavorLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        //删除数据是 同时涉及到两张表dish和dish_flavor
        List<Dish> dishList = this.listByIds(ids);
        for (Dish dish : dishList) {
            if(dish.getStatus()!=1){
                this.removeById(dish.getId());
                dishFlavorLambdaUpdateWrapper.eq(DishFlavor::getDishId,dish.getId());
                dishFlavorService.remove(dishFlavorLambdaUpdateWrapper);
            }else{
                throw new CustomException(dish.getName()+"在售,不能删除");
            }
        }
        dishFlavorLambdaUpdateWrapper.in(DishFlavor::getDishId,ids);*/
        
        //查询菜品状态。确定是否在售状态
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);

        int count = this.count(dishLambdaQueryWrapper);

        if(count>0){
            //说明在售，抛出一个异常
            throw new CustomException("套餐在售,无法删除");
        }

        //可以删除
        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);

        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);


    }
}
